package org.ix.dynamicapis.controller;

import org.ix.dynamicapis.annotation.SetFieldTypes;
import org.ix.dynamicapis.model.PageList;
import org.ix.dynamicapis.utils.ExcelUtils;
import org.ix.dynamicapis.model.request.ApiMultiConfigAddRequest;
import org.ix.dynamicapis.model.request.ApiMultiConfigPatchRequest;
import org.ix.dynamicapis.model.request.ApiMultiConfigQueryRequest;
import org.ix.dynamicapis.model.request.ApiMultiConfigSaveRequest;
import org.ix.dynamicapis.model.response.ApiMultiConfigDetailResponse;
import org.ix.dynamicapis.model.response.ApiMultiConfigListItemResponse;
import org.ix.dynamicapis.service.ApiMultiConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import java.io.IOException;
import java.net.URI;
import java.util.List;

@Api(tags = {"多主键测试相关接口"})
@CrossOrigin
@RequestMapping("/api-multi-config")
@RestController
@Validated
public class ApiMultiConfigController {

    private final ApiMultiConfigService apiMultiConfigService;

    @Autowired
    public ApiMultiConfigController(ApiMultiConfigService apiMultiConfigService) {
        this.apiMultiConfigService = apiMultiConfigService;
    }

    @ApiOperation(value = "添加多主键测试", notes = "添加多主键测试")
    @ApiImplicitParam(name = "apiMultiConfig", value = "添加多主键测试请求实体", required = true, dataType = "ApiMultiConfigAddRequest", paramType = "body")
    @PostMapping("/add")
    public ResponseEntity<ApiMultiConfigDetailResponse> add(@Validated @RequestBody @SetFieldTypes(types = {"create"}) ApiMultiConfigAddRequest apiMultiConfig) {
        ApiMultiConfigDetailResponse detail = apiMultiConfigService.add(apiMultiConfig);
        return ResponseEntity.created(URI.create("")).body(detail);
    }

    @ApiOperation(value = "根据ConfigKey, ConfigType更新多主键测试", notes = "根据ConfigKey, ConfigType更新多主键测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiMultiConfig", value = "更新多主键测试请求实体", required = true, dataType = "ApiMultiConfigSaveRequest", paramType = "body"),
            @ApiImplicitParam(name = "configKey", value = "ConfigKey", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "configType", value = "ConfigType", required = true, dataType = "int", paramType = "query")
    })
    @PatchMapping("/update")
    public ResponseEntity<ApiMultiConfigDetailResponse> updateByConfigKeyAndConfigType(
            @Validated @RequestBody @SetFieldTypes(types = {"update"}) ApiMultiConfigSaveRequest apiMultiConfig,
            @RequestParam("configKey") @NotBlank(message = "configKey(ConfigKey)不能为空") String configKey,
            @RequestParam("configType") @NotNull(message = "configType(ConfigType)不能为空") Integer configType
    ) {
        ApiMultiConfigDetailResponse detail = apiMultiConfigService.updateByConfigKeyAndConfigType(apiMultiConfig, configKey, configType);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation(value = "根据ConfigKey, ConfigType获取多主键测试详情", notes = "根据ConfigKey, ConfigType获取多主键测试详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configKey", value = "ConfigKey", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "configType", value = "ConfigType", required = true, dataType = "int", paramType = "query")
    })
    @GetMapping("/detail")
    public ResponseEntity<ApiMultiConfigDetailResponse> getByConfigKeyAndConfigType(
            @RequestParam("configKey") @NotBlank(message = "configKey(ConfigKey)不能为空") String configKey,
            @RequestParam("configType") @NotNull(message = "configType(ConfigType)不能为空") Integer configType
    ) {
        ApiMultiConfigDetailResponse detail = apiMultiConfigService.getByConfigKeyAndConfigType(configKey, configType);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation(value = "根据条件获取多主键测试列表", notes = "根据条件获取多主键测试列表")
    @GetMapping("/list")
    public ResponseEntity<List<ApiMultiConfigListItemResponse>> getList(ApiMultiConfigQueryRequest queryRequest) {
        return ResponseEntity.ok(apiMultiConfigService.getList(queryRequest));
    }

    @ApiOperation(value = "根据条件分页获取多主键测试列表", notes = "根据条件分页获取多主键测试列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "int", paramType = "query")
    })
    @GetMapping("/page-list")
    public ResponseEntity<PageList<ApiMultiConfigListItemResponse>> getPageList(
            ApiMultiConfigQueryRequest queryRequest,
            @RequestParam(value = "pageNum", defaultValue = "1") @Min(value = 1, message = "页码必须大于0") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "50") @Min(value = 1, message = "分页大小必须大于0") Integer pageSize
    ) {
        return ResponseEntity.ok(apiMultiConfigService.getPageList(queryRequest, pageNum, pageSize));
    }

    @ApiOperation(value = "根据条件导出多主键测试列表", notes = "根据条件导出多主键测试列表")
    @ApiImplicitParam(name = "exportName", value = "导出文件名", dataType = "String", paramType = "query")
    @GetMapping("/export")
    public ResponseEntity<?> export(HttpServletResponse response, ApiMultiConfigQueryRequest queryRequest,
                                    @RequestParam(value = "exportName", defaultValue = "多主键测试", required = false) String exportName) throws IOException {

        ExcelUtils.export(response, apiMultiConfigService.getList(queryRequest), ApiMultiConfigListItemResponse.class, exportName, "多主键测试");
        return null;
    }
}
